package calbs.socket.client;

import calbs.socket.tool.Byte2Hex;
import calbs.socket.tool.CHexConver;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

/**
 * Created by zhangqin on 2017-06-27.
 */
public class NettyClientBootstrap {
    private int port;
    private String host;
    private SocketChannel socketChannel;
    private static final EventExecutorGroup group = new DefaultEventExecutorGroup(20);
    public NettyClientBootstrap(int port, String host) throws InterruptedException {
        this.port = port;
        this.host = host;
        start();
    }
    private void start() throws InterruptedException {
        EventLoopGroup eventLoopGroup=new NioEventLoopGroup();
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE,true);
        bootstrap.group(eventLoopGroup);
        bootstrap.remoteAddress(host,port);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                socketChannel.pipeline().addLast(new IdleStateHandler(0,5,0));
                socketChannel.pipeline().addLast(new StringEncoder());
                socketChannel.pipeline().addLast(new Bytes2HexDecoder());
                socketChannel.pipeline().addLast(new StringDecoder());
                socketChannel.pipeline().addLast(new NettyClientHandler());
            }
        });
        ChannelFuture future =bootstrap.connect(host,port).sync();
        if (future.isSuccess()) {
            socketChannel = (SocketChannel)future.channel();
            System.out.println("connect server  成功---------");
        }
    }
    public static void main(String[]args) throws InterruptedException {
       // NettyClientBootstrap bootstrap=new NettyClientBootstrap(10088,"sk.wncalbs.com");
        NettyClientBootstrap bootstrap=new NettyClientBootstrap(10088,"127.0.0.1");

        byte[] login = CHexConver.hexStr2Bytes("7E7D20");
        ByteBuf loginresponseByteBuf = Unpooled.buffer(login.length);
        loginresponseByteBuf.writeBytes(login);
        bootstrap.socketChannel.writeAndFlush(loginresponseByteBuf);
 /*       byte[] bytes2 = CHexConver.hexStr2Bytes("7E7D0000010000007E7D".replace(" ",""));
        ByteBuf responseByteBuf2 = Unpooled.buffer(bytes2.length);
        responseByteBuf2.writeBytes(bytes2);
        bootstrap.socketChannel.writeAndFlush(responseByteBuf2);*/
 //7E7D2700020027F60000C350599F8AB30736AC7801DA3DDC000200FB49BD00240012D6870000000006506C6F7567687E7D
            Thread.sleep(1000);
            byte[] bytes = CHexConver.hexStr2Bytes("00000023AD0000C350001D757365726E616D653A746573743B70617373776F72643A3132333435367E7D");
            ByteBuf responseByteBuf = Unpooled.buffer(bytes.length);
            responseByteBuf.writeBytes(bytes);
            bootstrap.socketChannel.writeAndFlush(responseByteBuf);
        }
}
